package com.zzyl.nursing.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.domain.VisitRegistration;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.mapper.VisitRegistrationMapper;
import org.mybatis.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 预约登记Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-28
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper,Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;
    @Autowired
    private ElderMapper elderMapper;
    @Autowired
    private VisitRegistrationMapper visitRegistrationMapper;

    /**
     * 查询预约登记
     * 
     * @param id 预约登记主键
     * @return 预约登记
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询预约登记列表
     * 
     * @param reservation 预约登记
     * @return 预约登记
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {

        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约登记
     * 
     * @param reservation 预约登记
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation)
    {
        return save(reservation)?1:0;
    }

    /**
     * 到院
     * 
     * @param reservationDto 预约登记
     * @return 结果
     */
    @Transactional
    @Override
    public int updateReservation(ReservationDto reservationDto)
    {
        try {
            //如果来访时间在预约时间之前提示请输入正确的来访时间
            if (reservationDto.getTime().after(DateUtil.date(reservationDto.getVisitTime()))) {
                throw new BaseException("请输入正确的来访时间");
            }
            insetVisitRegistration(reservationDto);
            Reservation reservation = BeanUtil.toBean(reservationDto,Reservation.class);
            return updateById(reservation)?1:0;
        }catch (BaseException e){
            e.printStackTrace();
            throw e;
        }catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("更新预约信息失败", e);
        }
    }

    /**
     * 插入到 VisitRegistration
     * @param reservationDto
     */
    private void insetVisitRegistration(ReservationDto reservationDto) {
        VisitRegistration visitRegistration = new VisitRegistration();
        //如果预约类型为1是探访预约则查询相关老人的Id存入探访预约表中
        if (reservationDto.getType() == 1) {
           long elderId = elderMapper.selelectIdByName(reservationDto.getVisitor());
            if ( elderId == 0) {
                throw new RuntimeException("未找到指定的访问对象: " + reservationDto.getVisitor());
            }
           visitRegistration.setElderId(elderId);
        }
        visitRegistration.setType(reservationDto.getType());
        visitRegistration.setVisitorName(reservationDto.getName());
        visitRegistration.setVisitorMobile(reservationDto.getMobile());
        visitRegistration.setElderName(reservationDto.getVisitor());
        visitRegistration.setVisitTime(reservationDto.getVisitTime());
        visitRegistration.setCreateTime(DateUtils.getNowDate());
        int result = visitRegistrationMapper.insertVisitRegistration(visitRegistration);
        if (result <= 0) {
            throw new RuntimeException("插入访问登记记录失败");
        }
    }

    /**
     * 批量删除预约登记
     * 
     * @param ids 需要删除的预约登记主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除预约登记信息
     * 
     * @param id 预约登记主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id)
    {
        return removeById(id)?1:0;
    }

}
